<template>
  <div class="login-container">
    <div class="login-card">
      <h2 v-if="isRegister">注册</h2>
      <h2 v-else>登录</h2>
      <form @submit.prevent="onSubmit">
        <input v-model="username" placeholder="用户名" class="input" />
        <input v-model="password" type="password" placeholder="密码" class="input" />
        <input v-if="isRegister" v-model="confirmPassword" type="password" placeholder="确认密码" class="input" />
        <button type="submit" class="btn">{{ isRegister ? '注册' : '登录' }}</button>
      </form>
      <div v-if="error" class="error">{{ error }}</div>
      <div v-if="success" class="success">{{ success }}</div>
      <button @click="switchMode" class="switch-btn">
        {{ isRegister ? '已有账号？去登录' : '没有账号？去注册' }}
      </button>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
import UserStorage from '@/utils/userStorage'

export default {
  data() {
    return {
      username: '',
      password: '',
      confirmPassword: '',
      isRegister: false,
      error: '',
      success: ''
    }
  },
  methods: {
    async onSubmit() {
      this.error = ''
      if (this.isRegister) {
        if (this.password !== this.confirmPassword) {
          this.error = '两次密码不一致'
          return
        }
        try {
          const res = await axios.post('/boke-user/register', {
            username: this.username,
            password: this.password,
            confirmPassword: this.confirmPassword
          })
          this.success = '注册成功，请登录'
          this.error = ''
          this.isRegister = false
          this.password = ''
          this.confirmPassword = ''
        } catch (err) {
          this.error = err.response?.data || '注册失败'
          this.success = ''
        }
      } else {
        try {
          const res = await axios.post('/boke-user/login', {
            username: this.username,
            password: this.password
          })
          console.log(res)
          
          // 保存token和用户信息到localStorage
          UserStorage.saveToken(res.data.token)
          UserStorage.saveUserInfo({
            id: res.data.id,
            username: res.data.username,
            email: res.data.email,
            avatarUrl: res.data.avatarUrl,
            bio: res.data.bio
          })
          
          this.success = '登录成功'
          this.error = ''
          setTimeout(() => {
            this.$router.push('/')
          }, 800)
        } catch (err) {
          this.error = err.response?.data || '登录失败'
          this.success = ''
        }
      }
    },
    switchMode() {
      this.isRegister = !this.isRegister
      this.error = ''
      this.success = ''
    }
  }
}
</script>

<style scoped>
.login-container {
  min-height: 600px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-card {
  background: #fff;
  border-radius: 14px;
  box-shadow: 0 4px 32px rgba(66,185,131,0.08);
  padding: 40px 36px 28px 36px;
  width: 340px;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.input {
  width: 100%;
  margin-bottom: 18px;
  padding: 10px 14px;
  border-radius: 8px;
  border: 1px solid #e0e0e0;
  font-size: 1rem;
  outline: none;
  transition: border 0.2s;
}
.input:focus {
  border: 1.5px solid #42b983;
}
.btn {
  width: 100%;
  background: #42b983;
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 10px 0;
  font-size: 1.1rem;
  font-weight: bold;
  cursor: pointer;
  margin-bottom: 10px;
  transition: background 0.2s;
}
.btn:hover {
  background: #1890ff;
}
.switch-btn {
  background: none;
  border: none;
  color: #1890ff;
  cursor: pointer;
  margin-top: 8px;
  font-size: 0.98rem;
}
.error {
  color: #ff4d4f;
  margin-bottom: 8px;
  font-size: 0.98rem;
}
.success {
  color: #52c41a;
  margin-bottom: 8px;
  font-size: 0.98rem;
}
</style> 